package cn.com.fuhuitong.main.cate.ui.fragment

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.base.ViewModeFragment
import cn.com.fuhuitong.main.cate.adapter.CVT
import cn.com.fuhuitong.main.cate.adapter.CategoryChildAdapter
import cn.com.fuhuitong.main.cate.vm.CategoryViewModel
import cn.com.fuhuitong.main.home.ui.fragment.HomeFragment
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.vlayout.layout.GridLayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.http.HttpResponseStatus
import kotlinx.android.synthetic.main.fragment_category_child.*

class CategoryChildFragment : ViewModeFragment<CategoryViewModel>() {

    private var groupId: Int = 0

    companion object {
        private const val GROUP_ID = "GROUP_ID"
        fun newInstance(id: Int): CategoryChildFragment {
            val fragment = CategoryChildFragment()
            fragment.arguments = Bundle().apply {
                putInt(GROUP_ID, id)
            }
            return fragment
        }
    }

    override fun viewModel(): CategoryViewModel = ViewModelProvider(
        this, ViewModelProvider.AndroidViewModelFactory(BaseApplication.INSTANCE)
    ).get(CategoryViewModel::class.java)

    override val layoutResId: Int
        get() = R.layout.fragment_category_child

    override fun initView() {
        this.initViewRecyclerAdapter(
            recycler_category_child,
            arrayOf(CVT.CATEGORY_CHILD.ordinal)
        )

        delegateAdapter.addAdapter(initViewCategoryChildAdapter().apply {
            categoryChildAdapter = this
        })

        categoryChildAdapter.setOnItemClickListener { _, _, cateChildEntity ->
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_GOODS_LIST)
                .withSerializable(AppConstance.BUNDLE_CATE_CHILD, cateChildEntity)
                .navigation()
        }

        groupId = arguments?.getInt(GROUP_ID) ?: 0

    }

    override fun initViewMode() {

        viewModel.cateChildDataLiveData.observe(viewLifecycleOwner, Observer { response ->
            handlerResponseEmpty(
                status = response.status, message = response.statusTip,
                length = response.response?.data?.size ?: 0
            )
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                categoryChildAdapter.setAddDataNew(it)
            }
        })
    }

    override fun initData() {
        viewModel.cateChild(groupId)
    }


    /**
     * 初始化物品适配器
     * */
    private lateinit var categoryChildAdapter: CategoryChildAdapter
    private fun initViewCategoryChildAdapter(): CategoryChildAdapter {
        val helper = GridLayoutHelper(3)
        helper.setAutoExpand(false)
        helper.vGap = HomeFragment.dp2px(requireContext(), R.dimen.dp_10)
        helper.hGap = HomeFragment.dp2px(requireContext(), R.dimen.dp_10)
        helper.setPadding(
            HomeFragment.dp2px(requireContext(), R.dimen.dp_10),
            HomeFragment.dp2px(requireContext(), R.dimen.dp_20),
            HomeFragment.dp2px(requireContext(), R.dimen.dp_10),
            HomeFragment.dp2px(requireContext(), R.dimen.dp_20)
        )
        return CategoryChildAdapter(
            requireContext(), helper
        )
    }
}